home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Snippets / Development Tools & Languages / Shared Lib. Mgr. C++ / Sources / Process.cp next >
Encoding:
Text File  |  1992-08-30  |  5.1 KB  |  225 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1992 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Date: Wednesday, June 10, 1992 22:37:30
  5.   Revision comments are at the end of this file.
  6.   ---
  7.   TProcess is a Process Manager class.
  8.   Process.cp contains the class body information for the TProcess class member functions.
  9.   _________________________________________________________________________________________________________ */
  10.  
  11.  
  12. // Include files
  13. #ifndef _PROCESS_
  14. #include "Process.h"
  15. #endif
  16.  
  17.  
  18. // _________________________________________________________________________________________________________ //
  19. // TRandom class member function implementations
  20.  
  21. //    INITIATION ROUTINES
  22. void TProcess::Initialize(ProcessSerialNumber theNum)
  23. // Initialize the TProcess class with known values.
  24. {
  25.     OSErr anErr;
  26.     fFirstTime = true;                            // signal that we are inside constructor
  27.     fLast = false;
  28.     fProcessID.highLongOfPSN = theNum.highLongOfPSN;        
  29.     fProcessID.lowLongOfPSN  = theNum.lowLongOfPSN;
  30.  
  31.     anErr = GetCurrentProcess(&fMyProcessID);    // always get our own PSN
  32.  
  33.     this->IProcess();
  34.     fFirstPSN = fProcessID;                        // define the first one
  35.     fFirstTime = false;                            // signal this time is over
  36. }
  37.  
  38.  
  39. Boolean TProcess::IProcess()
  40. // We are using a special IProcess member function for initializing class fields to
  41. // known values.
  42. {
  43.     OSErr anErr;
  44.  
  45.     // get our own PSN number
  46.     if (fFirstTime)                                // our first PSN (our own)
  47.     {
  48.         anErr = GetNextProcess(&fProcessID);
  49.  
  50.         if (anErr != noErr)
  51.             return false;
  52.  
  53.         return true;
  54.     }
  55.     else
  56.         anErr = GetNextProcess(&fProcessID);    // fetch other PSNs
  57.     {
  58.         if (anErr != noErr)
  59.         {
  60.             fLast = true;
  61.             this->First();
  62.             return false;
  63.         }
  64.         else
  65.             return true;
  66.     }
  67. }
  68.  
  69.  
  70. //     MAIN INTERFACES
  71. Boolean TProcess::KillApplication(ProcessSerialNumber* thePSN)
  72. // Quit the application which is defined by the PSN.
  73. {
  74.     OSErr anErr;
  75.     AEAddressDesc target;
  76.     AppleEvent theAE,  theAEReply;
  77.  
  78.     theAE.dataHandle = theAEReply.dataHandle = target.dataHandle = NULL;
  79.  
  80.     anErr = AECreateDesc(typeProcessSerialNumber, (Ptr)thePSN, sizeof(ProcessSerialNumber), &target);
  81.     if (anErr != noErr)
  82.         return false;
  83.  
  84.     anErr = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &target, kAutoGenerateReturnID, kAnyTransactionID, &theAE);
  85.  
  86.     if (anErr != noErr)
  87.     {
  88.         AEDisposeDesc(&target);
  89.         return false;
  90.     }
  91.  
  92.     anErr = AESend(&theAE, &theAEReply, kAENoReply, kAENormalPriority, kNoTimeOut, NULL, NULL);
  93.  
  94.     AEDisposeDesc(&target);
  95.     AEDisposeDesc(&theAE);
  96.  
  97.     if (anErr != noErr)
  98.         return false;
  99.     else
  100.         return true;
  101. }
  102.  
  103.  
  104. short TProcess::GetNumProcesses()
  105. // Get the amount of currently running processes.
  106. {
  107.     ProcessSerialNumber aPSN;
  108.     short num = 0;
  109.  
  110.     aPSN.highLongOfPSN = 0;
  111.     aPSN.lowLongOfPSN = kNoProcess;
  112.  
  113.     while (GetNextProcess(&aPSN) == noErr)
  114.         num++;
  115.  
  116.     return num;
  117. }
  118.  
  119.  
  120. ProcessInfoRec TProcess::GetProcessInfoRec()
  121. // Return the full ProcInfoRec of specified process, NULL if we got into trouble.
  122. {
  123.     ProcessInfoRec theRec;
  124.     OSErr anErr;
  125.  
  126.     theRec.processName = NULL;
  127.     theRec.processAppSpec = NULL;
  128.     theRec.processInfoLength = sizeof(theRec);
  129.  
  130.     anErr = GetProcessInformation(&fProcessID, &theRec);
  131.  
  132.     return theRec;
  133. }
  134.  
  135.  
  136. unsigned long TProcess::GetProcessSize()
  137. // Return size of process.
  138. {
  139.     ProcessInfoRec temp = this->GetProcessInfoRec();
  140.     return temp.processSize;
  141. }
  142.  
  143.  
  144. unsigned long TProcess::GetFreeMem()
  145. // Return the amount of free memory available for the process.
  146. {
  147.     ProcessInfoRec temp = this->GetProcessInfoRec();
  148.     return temp.processFreeMem;
  149. }
  150.  
  151.  
  152. unsigned long TProcess::GetLaunchDate()
  153. // Return in seconds the point when the application was launched.
  154. {
  155.     ProcessInfoRec temp = this->GetProcessInfoRec();
  156.     return temp.processLaunchDate;
  157. }
  158.  
  159.  
  160. Boolean TProcess::FindProcess(OSType signature)
  161. // Find process with the right signature, style 'MACS'.
  162. {
  163.     ProcessInfoRec theRec;
  164.     fProcessID.highLongOfPSN = 0;
  165.     fProcessID.lowLongOfPSN = kNoProcess;        // start from beginning
  166.  
  167.     theRec.processName = NULL;
  168.     theRec.processAppSpec = NULL;
  169.     theRec.processInfoLength = sizeof(theRec);
  170.  
  171.     while (GetNextProcess(&fProcessID) == noErr)
  172.     {
  173.         if (GetProcessInformation(&fProcessID, &theRec) == noErr)
  174.         {
  175.             if (theRec.processSignature == signature)
  176.                 return true;                    // we found it
  177.         }
  178.     }
  179.     return false;                                // we didn't find the process, sigh
  180. }
  181.  
  182. ProcessSerialNumber TProcess::GetMyProcessID() const
  183. // Return the Process ID of the process running.
  184. {
  185.     return fMyProcessID;
  186. }
  187.  
  188.  
  189. ProcessSerialNumber TProcess::GetProcessID() const
  190. // Return the Process ID of the currently inspected process. 
  191. {
  192.     return fProcessID;
  193. }
  194.  
  195.  
  196. void TProcess::Next()
  197. // Return next process in the process list.
  198. {
  199.     this->IProcess();
  200. }
  201.  
  202.  
  203. Boolean TProcess::Last()
  204. // Return last process in the process list.
  205. {
  206.     return fLast;
  207. }
  208.  
  209.  
  210. void TProcess::First()
  211. // Return first process in the process list.
  212. {
  213.     fProcessID = fFirstPSN;
  214. }
  215.  
  216.  
  217.  
  218. // _________________________________________________________________________________________________________ //
  219.  
  220. /*    Change History (most recent last):
  221.   No        Init.    Date        Comment
  222.   1            khs        6/10/92        New file
  223.   2            khs        7/6/92        First decent working class
  224. */
  225.